package com.foreknow.controller;

import com.foreknow.jdbc.Student;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;


public class TestServlet_demo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");


        PrintWriter out = resp.getWriter();
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String school = req.getParameter("school");
        String email = req.getParameter("email");
        out.println(name);
        out.println(age);
        out.println(school);
        out.println(email);


        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection connection =  DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/elm?characterEncoding=UTF-8","root",
                    "root");
            PreparedStatement preparedStatement = connection.prepareStatement("insert into student_inf values (?,?,?,?);");
            preparedStatement.setObject(1,name);
            preparedStatement.setObject(2,age);
            preparedStatement.setObject(3,school);
            preparedStatement.setObject(4,email);

            preparedStatement.executeUpdate();


            preparedStatement.close();
            connection.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }



    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String school = req.getParameter("school");
        String email = req.getParameter("email");
        out.println(name);
        out.println(age);
        out.println(school);
        out.println(email);


        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection connection =  DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/elm?characterEncoding=UTF-8","root",
                    "root");
            PreparedStatement preparedStatement = connection.prepareStatement("insert into student_inf values (?,?,?,?);");
            preparedStatement.setObject(1,name);
            preparedStatement.setObject(2,age);
            preparedStatement.setObject(3,school);
            preparedStatement.setObject(4,email);

            preparedStatement.executeUpdate();


            preparedStatement.close();
            connection.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

//        //        实例化信息
//        Student student = new Student();
//        student.setName("张三");
//        student.setAge(18);
//        student.setEmail("1354678@qq.com");
//        student.setSchool("初中");
//
//
//
//        ArrayList<Object> list = new ArrayList<>();
//        Collections.addAll(list,student);
//
//
//
//
//
////        普通发送
//        //存入
//        req.setAttribute("list",list);
//        //发送
//        req.getRequestDispatcher("/formdemo.jsp").forward(req,resp);

        //创建session对象
        HttpSession session = req.getSession();
        session.setAttribute("name","zhangsan");

        //跳转到list.jsp页面
        req.getRequestDispatcher("/list.jsp").forward(req,resp);
    }
}
